package com.viroyal.monitorlib.service;

import android.accessibilityservice.AccessibilityService;
import android.annotation.SuppressLint;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.suntiago.baseui.utils.log.Slog;
import com.viroyal.monitorlib.utils.SPUtils;

import java.util.List;
import static android.view.accessibility.AccessibilityEvent.TYPE_VIEW_CLICKED;
import static android.view.accessibility.AccessibilityEvent.TYPE_VIEW_LONG_CLICKED;

public class AccessibilityMonitorService extends AccessibilityService {
    private static final String TAG = "AccessibilityMonitorSer";
    public String mWindowClassName;
    public String mCurrentPackage;

    @SuppressLint("SwitchIntDef")
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int type = event.getEventType();
        switch (type) {
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                mCurrentPackage = event.getClassName() == null ? "" : event.getClassName().toString();
                mWindowClassName = event.getPackageName() == null ? "" : event.getPackageName().toString();
                Slog.d(TAG,
                        "onAccessibilityEvent: getClassName:[" + mCurrentPackage + "],getPackageName:[" + mWindowClassName + "]");
                SPUtils.getInstance(getApplication()).put("pkg", mWindowClassName);
                SPUtils.getInstance(getApplication()).put("class", mCurrentPackage);
//                if (!event.getPackageName().toString().contains("packageinstaller") && UpdateApp.getContext().getPackageName() != mWindowClassName) {
//                    //将应用调用至前台
//                    ActivityManager activtyManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
//                    List<ActivityManager.RunningTaskInfo> runningTaskInfos = activtyManager.getRunningTasks(100);
//                    for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos) {
//                        if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName())) {
//                            activtyManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
//                            return;
//                        }
//                    }
//                }
                break;
            case TYPE_VIEW_CLICKED:
            case TYPE_VIEW_LONG_CLICKED:
                break;
        }

//        if (event.getPackageName().toString().contains("packageinstaller")) {
//            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
//            if (nodeInfo != null) {
//                if (!checkIfUnInstall(nodeInfo)) {
//                    iterateNodes(nodeInfo);
//                }
//            }
//        }
    }

    @Override
    public void onInterrupt() {

    }

    private void iterateNodes(AccessibilityNodeInfo nodeInfo) {
        String[] clickTexts = {"安装", "继续", "继续安装", "替换", "下一步", "仅允许一次", "确定","打开"};
        for (String clickText : clickTexts) {
            List<AccessibilityNodeInfo> infos = null;
            infos = nodeInfo.findAccessibilityNodeInfosByText(clickText);
            for (AccessibilityNodeInfo info : infos) {
                if (info.isClickable() && info.isEnabled()) {
                    info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
    }

    /**
     * 检查是否是卸载操作（如果安装界面出现“卸载”，那就无计咯）
     *
     * @param nodeInfo
     * @return
     */
    private Boolean checkIfUnInstall(AccessibilityNodeInfo nodeInfo) {
//        List<AccessibilityNodeInfo> infos = nodeInfo.findAccessibilityNodeInfosByText("卸载");
//        if (infos != null && !infos.isEmpty()) {
//            return true;
//        }
        return false;
    }
}